<template>
	<view class="proc-list-wrap">
		<cu-custom bgColor="bg-blue" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content" v-if="listConfig.listConfig">{{ listConfig.listConfig.service_view_name }}</block>
			<block slot="right"><text class="cuIcon-add" style="font-size: 40upx;margin-right: 20upx;"
					@click="toApply"></text></block>
		</cu-custom>
		<bx-list v-if="listConfig.serviceName" ref="bxList" showLabel pageType="proc"
			:rowButtons="listConfig.rowButtons" :serviceName="listConfig.serviceName" :condition="listConfig.condition"
			:listType="listConfig.listType" :viewTemp="listConfig.viewTemp" :listConfig="listConfig.listConfig"
			:searchWords="listConfig.searchVal" :searchColumn="'name'" :detailList="listConfig.detailList"
			:rownumber="10" :fixed="true" :top="200" :heightStyle="'calc(100vh-220rpx)'" :order="listConfig.order"
			@click-list-item="clickItem" @list-change="listChange" @clickFootBtn="clickFootBtn" @loadEnd="loadEnd">
		</bx-list>
		<view class="cu-modal bottom-modal" :class="showModal ? 'show' : ''">
			<view class="cu-dialog" style="max-height: 800rpx;">
				<view class="cu-bar bg-white">
					<view class="action text-green">确定</view>
					<view class="action text-blue" @tap="hideModal">取消</view>
				</view>
				<view class="padding-xl" v-if="listConfig.listConfig">
					<bxform ref="bxformApplyProc" :pageType="'add'" :fields="listConfig.listConfig.srv_cols"
						:BxformType="'add'" @value-blur="valueChange"></bxform>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import bxList from '@/components/bx-list/bx-list.vue';
	import bxform from '@/components/bx-form/bx-form.vue';
	export default {
		name: 'ProcList',
		components: {
			bxList,
			bxform
		},
		data() {
			return {
				publicButton: [],
				applyButton: {},
				showModal: false,
				listConfig: {
					// serviceName: 'srvoa_issue_info_select',
					// addService: 'srvoa_issue_info_add',
					detailList: false,
					condition: [],
					order: [{
						colName: 'create_time',
						orderType: 'desc'
					}],
					rowButtons: [],
					listType: 'proc',
					viewTemp: {
						title: 'issue_name',
						tip: '_executor_user_disp',
						img: null,
						price: 'proc_status',
						footer: 'create_time'
					},
					listConfig: null,
					searchVal: '',
					noData: false,
				}
			};
		},
		onReachBottom() {
			if (!this.noData) {
				this.$refs.bxList.loadData();
			}
		},
		onPullDownRefresh() {
			if (this.listConfig?.serviceName && this.$refs.bxList) {
				// this.getListV2();
				this.$refs.bxList.onRefresh();
			}
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 200)
		},
		methods: {
			loadEnd(e) {
				this.noData = true;
			},
			hideModal() {
				this.showModal = false;
			},
			toApply() {
				// this.$refs.popup.open()
				// this.showModal = true
				uni.navigateTo({
					url: '../apply/apply?serviceName=' + this.listConfig.addService
				});
			},
			async applyFlow() {
				//申请流程
				let serviceName = this.applyButton.serviceName;
				let req = [{
					serviceName: serviceName,
					data: [{}]
				}];
				let res = await this.onRequest('apply', serviceName, req, 'oa');
			},
			clickItem(e) {
				// console.log(e);
			},
			listChange(e) {
				// console.log(e);
				this.noData = false
			},
			clickFootBtn(e) {
				// console.log(e);
				// debugger
				if (e.button.button_type === 'edit') {
					//跳转到编辑页面
				} else if (e.button.button_type === 'delete' || e.button.button_type === 'deleteproc') {
					//删除页面
					this.deleteItem(e);
				} else if (e.button.button_type === 'procdetail') {
					let url = `../procDetail/procDetail?proc_instance_no=${e.row.proc_instance_no}`
					if (e.row.appno) {
						url += `&appno=${e.row.appno}`
					}
					uni.navigateTo({
						url: url
					});
				} else if (e.button.button_type === 'urge') {
					// 流程催办
					const buttinfo = e.button
					const row = e.row
					var type = buttinfo.button_type;
					var requests = [];
					var request = {};
					request["serviceName"] = buttinfo.service_name;
					var cMap = {
						colName: "proc_instance_no",
						ruleType: "eq",
						value: row.proc_instance_no
					};
					request["condition"] = [cMap];
					requests.push(request);
					let url = this.getServiceUrl('oa', buttinfo.service_name, 'operate');
					uni.showModal({
						title: '提示',
						content: `确认进行${buttinfo.button_name||'此'}操作?`,
						success: (res) => {
							if (res.confirm) {
								this.$http.post(url, requests).then(res => {
									uni.showToast({
										title: res.data.resultMessage,
										icon: res.data.state === 'SUCCESS' ? 'success' : 'none'
									})
								})
							}
						}
					})
				} else {
					debugger
					uni.showToast({
						title: '暂不支持此操作'
					});
				}
			},
			deleteItem(e) {
				let proc_instance_no = e.row.proc_instance_no;
				let serviceName = e.button.service_name;
				let req = [{
					serviceName: serviceName,
					condition: [{
						colName: 'proc_instance_no',
						ruleType: 'eq',
						value: proc_instance_no
					}]
				}];
				let url = this.getServiceUrl('oa', serviceName, 'operate');
				this.$http
					.post(url, req)
					.then(res => {
						if (res.data.state === 'SUCCESS') {
							uni.showToast({
								title: '删除成功'
							});
							this.$refs.bxList.onRefresh();
						} else {
							uni.showToast({
								title: res.data.resultMessage
							});
						}
					})
					.catch(error => {
						console.error(error);
						uni.showToast({
							title: '删除失败',
							icon: 'none'
						});
					});
			},
			async getListV2() {
				let app = uni.getStorageSync('activeApp');
				let serviceName = this.listConfig.serviceName;
				// app = 'oa';
				let colVs = await this.getServiceV2(this.listConfig.serviceName, 'list', 'proclist', app);
				colVs.srv_cols = colVs.srv_cols.filter(item => item.in_list === 1);
				console.log('colVs', colVs);
				// if (serviceName.indexOf('issue_') !== -1) {
				//   colVs.rowButton = [
				//     {
				//       page_type: '流程列表',
				//       button_name: '流程详情',
				//       button_type: 'procdetail',
				//       service_name: 'srvoa_issue_info_select',
				//       service_view_name: '需求问题管理查询',
				//       permission: true
				//     }
				//   ];
				// } else if (serviceName.indexOf('task_') !== -1) {
				//   colVs.rowButton = [
				//     {
				//       page_type: '流程列表',
				//       button_name: '流程详情',
				//       button_type: 'procdetail',
				//       service_name: 'srvoa_project_task_plan_select',
				//       service_view_name: '任务计划跟踪列表',
				//       permission: true
				//     }
				//   ];
				// }

				this.listConfig.listConfig = colVs;
				this.publicButton = colVs.gridButton.filter(item => {
					if (item.permission === true) {
						switch (item.button_type) {
							case 'add':
								this.showAdd = true;
								return item;
								break;
							case 'select':
								this.showSearchBar = true;
								return item;
								break;
							case 'apply':
								this.showAdd = true;
								this.applyButton = item;
								return item;
								break;
						}
					}
				});
				return colVs;
			}
		},
		async onLoad(option) {
			let activeApp = uni.getStorageSync('activeApp');
			activeApp ? '' : uni.setStorageSync('activeApp', 'oa');
			if (option.destApp) {
				uni.setStorageSync('activeApp', option.destApp)
			}
			if (option.serviceName) {
				this.listConfig.serviceName = option.serviceName;
				if (this.listConfig.serviceName.indexOf('_select') !== -1) {
					this.listConfig.addService = this.listConfig.serviceName.replace('_select', '_add');
				}
				await this.getListV2();
				if (this.listConfig.serviceName.indexOf('project_task_plan') !== -1) {
					this.listConfig.viewTemp = {
						title: 'task_name',
						tip: 'executor',
						img: null,
						// price: 'issue_level',
						footer: 'create_time'
					};
				}
				if (option.viewTemp) {
					this.listConfig.viewTemp = JSON.parse(decodeURIComponent(option.viewTemp));
				}
				if (this.listConfig.serviceName.indexOf('issue_info') !== -1 || !option.serviceName) {
					this.listConfig.addService = 'srvoa_issue_info_add';
					this.listConfig.serviceName = 'srvoa_issue_info_select';
					this.listConfig.viewTemp = {
						title: 'issue_name',
						tip: '_executor_user_disp',
						img: null,
						price: 'proc_status',
						footer: 'create_time'
					};
				}
			} else {
				this.listConfig.addService = 'srvoa_issue_info_add';
				this.listConfig.serviceName = 'srvoa_issue_info_select';
				this.listConfig.viewTemp = {
					title: 'issue_name',
					tip: '_executor_user_disp',
					img: null,
					price: 'proc_status',
					footer: 'create_time'
				};
			}

		}
	};
</script>

<style lang="scss">
	.proc-list-wrap {
		// min-height: calc(100vh - var(--window-top));
		min-height: 100vh;
	}
</style>
